package com.example.spring_database.controller;
import com.example.spring_database.FileUpload;

import com.example.spring_database.entity.book;
import com.example.spring_database.mappers.bookMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/bookmanager")
public class BookController {
    @Resource
    bookMapper bookMapper;

    @GetMapping
    ("/login")
    String login_index(Model model){
        String bgimg="/index_bgpage.jpeg";
        model.addAttribute("bgimg",bgimg);
        return "log_in";
    }


    @PostMapping("/login")
    String login_to_index2(Model model, @RequestParam String username, @RequestParam String password, ServletRequest servletRequest, ServletResponse servletResponse) throws IOException {
        if(username.equals("xwq")&&password.equals("2019211524")) {
            List<book> books=bookMapper.selectAll();
            model.addAttribute("books",books);
            HttpServletRequest req = (HttpServletRequest) servletRequest;
            HttpSession session = req.getSession();
            session.setAttribute("login_state","logged");
            return "book_list";
        }
        return "login_failed";
    }

    @GetMapping("/logout")
    String logout(ServletRequest servletRequest){
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpSession session = req.getSession();
        if(session.getAttribute("login_state")!=null){
            session.removeAttribute("login_state");
        }
        return "log_in";
    }


    @GetMapping("/home")
    String home_index(Model model){
        List<book> books=bookMapper.selectAll();
        model.addAttribute("books",books);
        return "book_list";
    }
/*大概没啥用了
    @PostMapping("/home")
    String login_to_index(Model model, @RequestParam String username, @RequestParam String password,ServletRequest servletRequest){
        if(username.equals("xwq")&&password.equals("2019211524")) {
            List<book> books=bookMapper.selectAll();
            model.addAttribute("books",books);
            HttpServletRequest req = (HttpServletRequest) servletRequest;
            HttpSession session = req.getSession();
            session.setAttribute("login_state","logged");
            return "book_list";
        }
        return "login_failed";
    }


 */


    @GetMapping("/add")
    String add_book_view(){return "book_add";}

    @PostMapping("/add")
    String add_book_action(Model model, @RequestParam String book_name,
                           @RequestParam String auther, @RequestParam String press, @RequestParam int year, @RequestParam int month, @RequestParam int store,@RequestParam("filename") MultipartFile image_file) throws IOException {
        if(!book_name.equals("")&&!auther.equals("")&&!press.equals("")){
            if(year<=2021&&year>0&&month<=12&&month>0){
                book new_book=new book();
                new_book.book_name=book_name;
                new_book.press=press;
                new_book.month=month;
                new_book.year=year;
                new_book.store=store;
                new_book.auther=auther;
                FileUpload fileUpload=new FileUpload();
                new_book.filename=fileUpload.upload_file(image_file,"/Users/xubin/Desktop/web-book-images");
                bookMapper.add(new_book);

                return "book_add_successful";
            }
        }
        return "book_add_failed";
    }

    @GetMapping("/delete")
    String delete(@RequestParam int book_id){
        bookMapper.deleteById(book_id);
        return "delete_successful";
    }

    @GetMapping("/bookinfo")
    String show_one_book(Model model,@RequestParam int book_id){
        book books=bookMapper.select_one_book(book_id);
        model.addAttribute("book_to_show", books);
        return "book_show";
    }

    @GetMapping("/change")
    String change_one_book_view(Model model,@RequestParam int book_id){
        book books=bookMapper.select_one_book(book_id);
        model.addAttribute("book_to_change", books);
        return "book_change";
    }

    @PostMapping("/change")
    String change_one_book(Model model,@RequestParam int book_id,@RequestParam String book_name,
                           @RequestParam String auther,@RequestParam String press,@RequestParam int year,@RequestParam int month,@RequestParam int store,@RequestParam("filename") MultipartFile image_file) {
        if(!book_name.equals("")&&!auther.equals("")&&!press.equals("")){
            if(year<=2021&&year>0&&month<=12&&month>0){
                book new_book=new book();
                new_book.book_name=book_name;
                new_book.press=press;
                new_book.month=month;
                new_book.year=year;
                new_book.store=store;
                new_book.auther=auther;
                new_book.id=book_id;
                FileUpload fileUpload=new FileUpload();
                new_book.filename=fileUpload.upload_file(image_file,"/Users/xubin/Desktop/web-book-images");
                bookMapper.update(new_book);
                model.addAttribute("book_id",book_id);
                return "book_change_successful";
            }
        }
        model.addAttribute("book_id",book_id);
        return "book_change_failed";
    }
}
